// logs.ts
// const util = require('../../utils/util.js')
import { formatTime } from '../../utils/util'
import {
  sm2ComparePublicKeyHex,
  sm2CompressPublicKeyHex,
  sm2Decrypt,
  sm2Encrypt,
  sm2GenerateKeyPairHex,
  sm2Signature,
  sm2VerifyPublicKey,
  sm2VerifySignature,
  sm3Hash,
  sm4Decrypt,
  sm4Encrypt,
  sm4GenerateKey
} from '../../utils/smCryptoUtils';

Component({
  data: {
    logs: [],
  },
  lifetimes: {
    attached() {
      this.setData({
        logs: (wx.getStorageSync('logs') || []).map((log: string) => {
          return {
            date: formatTime(new Date(log)),
            timeStamp: log
          }
        }),
      })
    },
    ready() {

      // const text = '_测_fasde-2023@试_';
      // console.log('原始数据:', text);
      //
      // console.log('sm3Hash:', sm3Hash(text));
      // console.log('------------------------------------------------');
      //
      // const key = sm4GenerateKey();
      // console.log('key:', key);
      // const iv = sm4GenerateKey();
      // console.log('iv:', iv);
      //
      // // let encryptData = sm4Encrypt(text, key); // 加密，默认输出 16 进制字符串，默认使用 pkcs#7 填充（传 pkcs#5 也会走 pkcs#7 填充）
      // let encryptData = sm4Encrypt(text, key, iv); // 加密，cbc 模式
      // console.log('encryptData_原始Hex:', encryptData);
      //
      // let decryptData = sm4Decrypt(encryptData, key, iv); // 解密，cbc 模式
      // console.log('sm4Decrypt:', decryptData);
      // console.log('------------------------------------------------');
      //
      // let keypair = sm2GenerateKeyPairHex();
      //
      // const publicKey = keypair.publicKey; // 公钥
      // const privateKey = keypair.privateKey; // 私钥
      // console.log('publicKey:', publicKey);
      // console.log('privateKey:', privateKey);
      //
      // // 默认生成公钥 130 位太长，可以压缩公钥到 66 位
      // const compressedPublicKey = sm2CompressPublicKeyHex(publicKey); // compressedPublicKey 和 publicKey 等价
      // console.log('compressedPublicKey:', compressedPublicKey);
      // const isEqu = sm2ComparePublicKeyHex(publicKey, compressedPublicKey); // 判断公钥是否等价
      // console.log('comparePublicKeyHex:', isEqu);
      //
      // console.log('verifyPublicKey1:', sm2VerifyPublicKey(publicKey));
      // console.log('verifyPublicKey2:', sm2VerifyPublicKey(compressedPublicKey));
      // console.log('------------------------------------------------');
      //
      // encryptData = sm2Encrypt(text, publicKey); // 加密结果
      // console.log('encryptData:', encryptData);
      // let compressed_encryptData = sm2Encrypt(text, compressedPublicKey); // 加密结果
      // console.log('compressed_encryptData:', compressed_encryptData);
      // decryptData = sm2Decrypt(encryptData, privateKey); // 解密结果
      // console.log('decryptData:', decryptData);
      //
      // // const result = '';
      // // console.log('decryptData:', sm2Decrypt(result, 'e4c6f10cb1a242e38c4b4eaff69cecba557882966e0d4d4ef4541175531695c1'));
      // console.log('------------------------------------------------');
      //
      // // 纯签名 + 生成椭圆曲线点
      // let sigValueHex = sm2Signature(text, privateKey); // 签名
      // console.log('sigValueHex:', sigValueHex);
      // let verifyResult = sm2VerifySignature(text, sigValueHex, publicKey); // 验签结果
      // console.log('verifyResult:', verifyResult);

      // 原始数据和密钥
      const text = '_测_fasde-2023@试_';
      const key = 'c2b34265b8f7c7908685eecda77e825f';
      const iv = 'd534564cdd62927fc78205c6b12b4291';
      const publicKey = "047fea744a02dd5146800f8170a5703066cfe5cd746da3e0f1f12941040299d512023362574282d592aa470d93ecedab6152c27ef52d0b44824084321e413f5fbd";
      const privateKey = "15e95cef57521b8181f3e6e1f238dcb9378dec65c5467a95198e39724e6f66bc";

      console.log('原始数据: ' + text);
      console.log('key: ' + key);
      console.log('iv: ' + iv);
      console.log('publicKey: ' + publicKey);
      console.log('privateKey: ' + privateKey);

      console.log('sm3Hash: ' + sm3Hash(text));
      console.log('------------------------------------------------');

      const encryptData = sm4Encrypt(text, key, iv);
      console.log('sm4Encrypt: ' + encryptData);
      console.log('sm4Decrypt: ' + sm4Decrypt(encryptData, key, iv));
      console.log('------------------------------------------------');

      // SM2 加密解密
      const sm2EncryptData = sm2Encrypt(text, publicKey);
      console.log('encryptData: ' + sm2EncryptData);
      console.log('decryptData: ' + sm2Decrypt(sm2EncryptData, privateKey));
      console.log('------------------------------------------------');

      // SM2 签名验签
      const sigValueHex = sm2Signature(text, privateKey);
      console.log('sigValueHex: ' + sigValueHex);
      console.log('verifyResult: ' + sm2VerifySignature(text, sigValueHex, publicKey));

    }
  },
})
